import { PostProcessStage, Viewer } from "cesium";
import { snowFS, rainFS, fogFS, overcastFS } from "./shader/weather"

export type Weather = {
    snow: PostProcessStage | null,
    rain: PostProcessStage | null,
    fog: PostProcessStage | null,
    overcast: PostProcessStage | null
}

export class WeatherUtils {
    weather: Weather = {
        snow: null,
        rain: null,
        fog: null,
        overcast: null
    }
    constructor(private viewer: Viewer) {

    }
    /**
     * 启用雪天天气
     * @param enable 是否启用
     */
    enableSnowWeather(enable: boolean) {
        if(enable) {
            if(!this.weather.snow) { // 没有雪我才去添加，有雪了就不管了
                this.weather.snow = new PostProcessStage({
                    name: "snow",
                    fragmentShader: snowFS
                })
                this.viewer.scene.postProcessStages.add(this.weather.snow);
            }
        }else {
            if(this.weather.snow) {
                this.viewer.scene.postProcessStages.remove(this.weather.snow);
                this.weather.snow = null;
            }
        }
    }
    /**
     * 启用雪天天气
     * @param enable 是否启用
     */
    enableRainWeather(enable: boolean) {
        if(enable) {
            if(!this.weather.rain) { // 没有雪我才去添加，有雪了就不管了
                this.weather.rain = new PostProcessStage({
                    name: "rain",
                    fragmentShader: rainFS
                })
                this.viewer.scene.postProcessStages.add(this.weather.rain);
            }
        }else {
            if(this.weather.rain) {
                this.viewer.scene.postProcessStages.remove(this.weather.rain);
                this.weather.rain = null;
            }
        }
    }
    /**
     * 启用雾天天气
     * @param enable 是否启用
     */
     enableFogWeather(enable: boolean) {
        if(enable) {
            if(!this.weather.fog) { // 没有雪我才去添加，有雪了就不管了
                this.weather.fog = new PostProcessStage({
                    name: "fog",
                    fragmentShader: fogFS
                })
                this.viewer.scene.postProcessStages.add(this.weather.fog);
            }
        }else {
            if(this.weather.fog) {
                this.viewer.scene.postProcessStages.remove(this.weather.fog);
                this.weather.fog = null;
            }
        }
    }
    /**
     * 启用阴天天气
     * @param enable 是否启用
     */
     enableOvercastWeather(enable: boolean) {
        if(enable) {
            if(!this.weather.overcast) { // 没有雪我才去添加，有雪了就不管了
                this.weather.overcast = new PostProcessStage({
                    name: "overcast",
                    fragmentShader: overcastFS
                })
                this.viewer.scene.postProcessStages.add(this.weather.overcast);
            }
        }else {
            if(this.weather.overcast) {
                this.viewer.scene.postProcessStages.remove(this.weather.overcast);
                this.weather.overcast = null;
            }
        }
    }
}